package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

class ShinyView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {

    private val textContent = "Hello,World!"

    private var mWidth = 0
    private var mHeight = 0

    private var translate = 0F

    private val textBound by lazy {
        Rect()
    }

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            textSize = context.resources.getDimension(R.dimen.sp_20)
            style = Paint.Style.FILL
        }
    }

    private var mGradient: LinearGradient? = null

    private val mMatrix by lazy {
        Matrix()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_200).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)

        if (mGradient == null) {
            mGradient = LinearGradient(
                0F,
                0F,
                mWidth / 6.0F,
                mHeight / 8.0F,
                intArrayOf(
                    //起始颜色
                    resources.getColor(R.color.color_da8989),
                    //中间颜色
                    resources.getColor(R.color.color_ffcccccc),
                    //末尾颜色
                    resources.getColor(R.color.color_da8989)
                ),
                floatArrayOf(0F, 0.5f, 1F),
                Shader.TileMode.CLAMP
            )
        }

        mPaint.shader = mGradient

        mPaint.getTextBounds(textContent, 0, textContent.length, textBound)

        translate = -mWidth / 2.0F
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(resources.getColor(R.color.color_F3F3F3))

        val fontHeight = mPaint.fontMetrics.bottom - mPaint.fontMetrics.top
        //val textX = mWidth / 2.0F - textBound.width() / 2.0F
        //val textY = mHeight - (mHeight - fontHeight) / 2 - mPaint.fontMetrics.bottom

        canvas?.apply {
            translate(mWidth / 2.0F, mHeight / 2.0F)
            val textX0 = -textBound.width() / 2.0F
            val textY0 = -mHeight / 2.0F - (-mHeight / 2.0F - fontHeight) / 2 - mPaint.fontMetrics.bottom
            drawText(textContent, textX0, textY0, mPaint)
            save()
            rotate(180F)
            //val textX1 = -textBound.width() / 2.0F
            //val textY1 = -mHeight / 2.0F - (-mHeight / 2.0F - fontHeight) / 2 - mPaint.fontMetrics.bottom
            drawText(textContent, textX0, textY0, mPaint)
            restore()
        }

        translate += (textBound.width() / 2.0F + mWidth / 2.0F) / 20F
        translate = translate.coerceAtMost(textBound.width() / 2.0F)
        mMatrix.setTranslate(translate, 0F)
        mGradient?.setLocalMatrix(mMatrix)
        postInvalidateDelayed(250)
        if (translate == textBound.width() / 2.0F) {
            translate = -mWidth / 2.0F
        }
    }
}